package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.TwoMyBaseAdapter;
import com.example.zzl.custom.MyTag;
import com.example.zzl.databinding.InfoCommodityCommentLvBinding;
import com.example.zzl.model.bean.Info;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/14.
 */

public class InfoLvAdapter extends TwoMyBaseAdapter<Info.GetInfoReviewBean.ResultBeanXX, InfoLvAdapter.ViewHolder> {


    public InfoLvAdapter(Context context, List<Info.GetInfoReviewBean.ResultBeanXX> list) {
        super(context, list);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        InfoCommodityCommentLvBinding lvDb = DataBindingUtil.inflate(inflater, R.layout.info_commodity_comment_lv, parent, false);
        ViewHolder viewHolder = new ViewHolder(lvDb);
        viewHolder.lvDb = lvDb;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Info.GetInfoReviewBean.ResultBeanXX result = list.get(position);
        holder.lvDb.setResult(result);
        final ImageView iv = holder.lvDb.iv;
        iv.setImageBitmap(null);

        iv.setTag(new MyTag(result.user.strHeadImage, position));
        VolleySingleton.volleySingleton.getImageLoader().get(result.user.strHeadImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (((MyTag) iv.getTag()).tag.equals(response.getRequestUrl())) {
                    iv.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        holder.db.executePendingBindings();
    }

    class ViewHolder extends TwoMyBaseAdapter.BaseHolder {

        InfoCommodityCommentLvBinding lvDb;

        public ViewHolder(ViewDataBinding db) {
            super(db);
        }
    }
}
